package iscript.gameFramework.IsoLibarys 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObjectContainer;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import flash.utils.Timer;
	import iscript.coreFramework.data.datatable.DataTable;
	import iscript.coreFramework.displays.drawing.LineSegment;
	import iscript.coreFramework.displays.Element;
	import iscript.coreFramework.layout.AlignLayout;
	import iscript.coreFramework.math.Point2D;
	import iscript.coreFramework.reflect.getClassName;
	import iscript.coreFramework.util.GUID;
	import iscript.coreFramework.util.StringUtils;
	import iscript.gameFramework.IsoLibarys.config.IsoConfig;
	import iscript.gameFramework.IsoLibarys.data.IsoBound;
	import iscript.gameFramework.IsoLibarys.data.IsoBaseData;
	import iscript.gameFramework.IsoLibarys.data.IsoSpriteBuffer;
	import iscript.gameFramework.IsoLibarys.data.IsoSpriteBufferNode;
	import iscript.gameFramework.IsoLibarys.data.IsoViewManager;
	import iscript.gameFramework.IsoLibarys.display.IsoBitmapData;
	import iscript.gameFramework.IsoLibarys.display.IsoScenePane;
	import iscript.gameFramework.IsoLibarys.math.IsoMath;
	import iscript.gameFramework.IsoLibarys.state.IsoRunTimeState;
	import iscript.gameFramework.IsoLibarys.state.IsoSceneState;
	import iscript.uiFramework.baseShape.Ball;


	public class IsoScene extends Sprite
	{
		
		static public const  ACTIVEISOVIEW:String = "ActiveIsoView";
		
		public function IsoScene(gridSize:Number,gridWidth:Number,gridHeight:Number)
		{			
			_gridType = IsoType.DIAMOND;
			this._gridSize = gridSize;
			this.gridWidth = gridWidth;
			this.gridHeight = gridHeight;
			this.initialize();
			_isoViewContainer = new Sprite();
			addChild(_isoViewContainer);
			
			
			
			IsoBitmapData.initialize(graphicsGridWidth, graphicsGridHeight);
			
			setRunTimeState(IsoRunTimeState.EDITORIDE);
			

			
		/*	
		   _isoViewContainerMask = new Sprite();
			addChild(_isoViewContainerMask)
		   var line:LineSegment = new LineSegment()
			line.drawRectangle(_isoViewContainerMask.graphics, new Rectangle(0,0,this.graphicsViewWidth,this.graphicsViewHeight));*/
				
		}
		
		public function getIsoSceneBound():IsoBound {
			return  new IsoBound(new Point2D(0, 0), new Point2D(_gridSize-1, _gridSize-1));
			}
		
		private var _runtimeState:String;
		
		/**
		 * IsoRunTimeState
		 * @param	runtimeState
		 */
		public function setRunTimeState(isoRunTimeState:String):void {			  
			  _runtimeState = isoRunTimeState;    
			  switch(isoRunTimeState) {
				    case  IsoRunTimeState.EDITORIDE:
					   IsoConfig.AutoRemoveIsoSprite = true;
					break;
				  case  IsoRunTimeState.GAMEAPP:
                       IsoConfig.AutoRemoveIsoSprite = false;
					break;
				  }
			}
			
		public function getRunTimeState():String {
			 return _runtimeState;
			}
 
		
		
 
		
		
		public function get allowAddIsoSprite():Boolean {
			  return  IsoSpriteBuffer.getInstance().count < IsoConfig.AllowBufferCount;
			}
		
		
		private var _isoViewContainer:Sprite;
		private var _isoViewContainerMask:Sprite;
		
		private var _gridType:String;
		private var _gridSize:int;
		public function get gridSize():int { return _gridSize;}		
		public function set gridSize(value:int):void { _gridSize = value; }
		
	 
		private var _gridWidth:Number = 0;
		private var _gridHeight:Number = 0;
		private var _graphicsGridWidth:Number;
		private var _graphicsGridHeight:Number;
		
		public function get graphicsViewWidth():Number {
			  return graphicsGridWidth * gridSize;		
			}

		public function get graphicsViewHeight():Number {
			  return graphicsGridHeight * gridSize;		
			}
		public  function  set gridWidth(value:Number):void { 
			                             _gridWidth = value;
										 _graphicsGridWidth = value * IsoMath.scaleX;
										 }
										 
		public  function  get gridWidth():Number { return _gridWidth; }
		 
		public  function  set gridHeight(value:Number):void { 
			                             _gridHeight = value;
										 _graphicsGridHeight = value * IsoMath.scaleY;
										 }
		public  function  get gridHeight():Number { return _gridHeight; }
         
		/**
		 * 容器的实体宽度
		 */
		public  function  get graphicsGridWidth():Number { return _graphicsGridWidth; }
		
		/**
		 * 容器的实体高度
		 */
		public  function  get graphicsGridHeight():Number { return _graphicsGridHeight; }
		 
		private var _tableData:DataTable;
		 
		public function  getGridData():DataTable {return _tableData;}
		 
			
		private function initialize():void {
			   //IsoType.DIAMOND 默认菱形地图 			   
			   var _currentGrid:Point2D = getOriginalVector();	
			   var  columna:Vector.<String> = new Vector.<String>();
			   var _width:Number =  _gridSize * _graphicsGridWidth;
			   var _height:Number =  _gridSize * _graphicsGridHeight;
			   
			   for (var i:int = 0; i <_gridSize; i++) 
			   {
				   columna.push(i.toString());				   
			   } 
			   _tableData = new DataTable(columna);

				for (var r:int = 0; r <_gridSize ; r++) 
				{
				   var rowData:Dictionary = new Dictionary();
					   for (var c:int = 0; c <_gridSize ; c++) 
					   {
						   var _isoSceneGrid:IsoBaseData = new IsoBaseData();
							   _isoSceneGrid.IsBuilder = false;
							   _isoSceneGrid.IsWalkable = false;
							   
							 //if (_buildermap != null) {
								//_isoSceneGrid.IsBuilder  = Boolean(_buildermap.getCell(r, c.toString()));
								//}	  
							   
						     if ( c == 0) {
							   _isoSceneGrid.Bound =  new Rectangle( _currentGrid.x - _graphicsGridWidth / 2 * (r + 1),
																	 _currentGrid.y + _graphicsGridHeight / 2 * r,
																	 _graphicsGridWidth,
																	 _graphicsGridHeight
																	 );
							  }else{
							   _isoSceneGrid.Bound =  new Rectangle( IsoBaseData(rowData[(c - 1).toString()]).Bound.x+_graphicsGridWidth/2, 
																	 IsoBaseData(rowData[(c - 1).toString()]).Bound.y+_graphicsGridHeight/2,
																	 _graphicsGridWidth,
																	 _graphicsGridHeight
																	 );
							  }
							 rowData[c.toString()] = _isoSceneGrid;
					   }
					 _tableData.addRow(rowData);					
				}
				
				
		
				
			}
		 
/*		private  var _isoscenePane:IsoScenePane;
		public function  clip(_width:Number, _height:Number):void {	
			  _isoscenePane = new IsoScenePane(_isoViewContainer,_isoViewContainerMask);
			  _isoscenePane.mask(graphicsGridWidth * gridSize, graphicsGridHeight * gridSize, _width, _height);
			  _isoscenePane.scrollDrag = true;
			  

			}*/
			
		/**
		 * 网格原始坐标
		 * @return
		 */	
		public function  getOriginalVector():Point2D {
			 return new Point2D(_graphicsGridWidth*_gridSize/2,0);
			}
		 
		/*View*/
		
		

		private var _isoViewManager:IsoViewManager = new IsoViewManager();
		
		public function get isoViewManager():IsoViewManager { return _isoViewManager; }
		
		public function addIsoView(keyName:String,isoview:IsoView):void {
			     if (keyName == null) keyName = GUID.create();
			     var result:Boolean =  _isoViewManager.add(keyName, isoview);
				 if (result)
				 _isoViewContainer.addChild(isoview);
				 
			}
			
		public function activeIsoView(target:*):void {
			  _isoViewManager.activeIsoView(target);
			}

		public function getActiveView():IsoView { 
			 	return _isoViewManager.getActiveIsoView();
			}	
			
			
		public function dispose():void {
			_tableData.dispose();
			_tableData = null;
			_isoViewManager.dispose();
			}
			
			
		/* private var _state:String;
		
		public function setState(state:String):void { 
			    _state = state;
				
			   switch(_state) {
				case IsoSceneState.NORMAL:
					break;
				case IsoSceneState.BUILD:
					break;
				}
			}		 
		public function getState():String { return _state; }*/
		
			
	}

}